package lumingweihua.future.cn.lumingweihua.person;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Intents;
import com.zhusx.core.utils._Lists;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.SimpleHttpListener;
import lumingweihua.future.cn.lumingweihua.network.UserManager;
import lumingweihua.future.cn.lumingweihua.ui.BaseAppFragment;
import lumingweihua.future.cn.lumingweihua.ui.message.MessageActivity;
import lumingweihua.future.cn.lumingweihua.ui.message.domain.CountEntity;
import lumingweihua.future.cn.lumingweihua.ui.mine.GoodsBossActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.InviteFriendActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.MineCarActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.MineCarSourceActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.MineGoodsSourceActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.MineInfoActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.MineRushActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.ReviewProgressActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.UseManagerActivity;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.PhoneData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.UserInfoData;
import lumingweihua.future.cn.lumingweihua.widget.FrescoImageView;
import lumingweihua.future.cn.lumingweihua.widget.dialog.BottomListDialog;

/**
 * Author        zhusx
 * Created       2018/3/16 17:13
 */
public class MyFragment extends BaseAppFragment {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_head)
    FrescoImageView imageView;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.iv_read)
    View ivRead;

    LoadData<UserInfoData> userData;
    LoadData<List<PhoneData>> phoneData;
    public static final int REQUEST_ID = 312;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_me, container, false);
    }

    private boolean isCreate;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        isCreate = true;
        init();
    }

    private void init() {
        userData = new LoadData<>(LoadData.Api.个人中心, this);
        userData._setOnLoadingListener(new SimpleHttpListener<UserInfoData>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> request, IHttpResult<UserInfoData> result) {
                UserManager.getInstance().avatar_url = result.getData().avatar_url;
                if (result.getData().nickname != null && result.getData().nickname.length() > 6) {
                    result.getData().nickname = result.getData().nickname.substring(0, 6);
                }
                UserManager.getInstance().nickname = result.getData().nickname;
                UserManager.getInstance().approve = result.getData().approve;
                UserManager.getInstance().carAuth = result.getData().carAuth;
                UserManager.getInstance().inviteTime = result.getData().inviteTime;
                tvName.setText(result.getData().nickname);
                imageView.setImageURI(result.getData().avatar_url);
                switch (String.valueOf(result.getData().approve)) {
                    case "1"://待审核
                        tvStatus.setText("待审核");
                        tvStatus.setTextColor(getResources().getColor(R.color.font_blue));
                        break;
                    case "2"://认证通过
                        tvStatus.setText("已认证");
                        tvStatus.setTextColor(getResources().getColor(R.color.font_blue));
                        break;
                    case "3"://认证失败
                        tvStatus.setText("认证失败");
                        tvStatus.setTextColor(getResources().getColor(R.color.font_blue));
                        break;
                    case "4"://未认证
                    default:
                        tvStatus.setText("未认证");
                        tvStatus.setTextColor(getResources().getColor(R.color.font_blue));
                        break;
                }

            }
        });
        refreshInfo();
        phoneData = new LoadData<>(LoadData.Api.个人中心热线号码, this);
        phoneData._setOnLoadingListener(new SimpleHttpListener<List<PhoneData>>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> request, IHttpResult<List<PhoneData>> t) {
                showPhones(t.getData());
            }
        });
        LoadData<CountEntity> countData = new LoadData<>(LoadData.Api.系统通知数量, this);
        countData._setOnLoadingListener(new SimpleHttpListener<CountEntity>() {
            @Override
            public void onLoadComplete(LoadData.Api api, HttpRequest<Object> request, IHttpResult<CountEntity> t) {
                if (!"0".equals(t.getData().message_num) || !"0".equals(t.getData().carriage_num)) {
                    ivRead.setVisibility(View.VISIBLE);
                }
            }
        });
        countData._loadData();
    }

    @OnClick({R.id.layout_user, R.id.tv_car, R.id.tv_status, R.id.tv_push_goods, R.id.tv_push_car, R.id.tv_order, R.id.tv_phones, R.id.tv_use, R.id.tv_friend, R.id.iv_message})
    public void onClick(View v) {
        switch (v.getId()) {
            //头像
            case R.id.layout_user:
                startActivityForResult(new Intent(v.getContext(), MineInfoActivity.class), REQUEST_ID);
                break;
            //我的车辆
            case R.id.tv_car:
                startActivity(new Intent(v.getContext(), MineCarActivity.class));
                break;
            //我是货主 已认证
            case R.id.tv_status:
                if (!userData._hasCache()) {
                    userData._reLoadData();
                    return;
                }
                switch (String.valueOf(UserManager.getInstance().approve)) {
                    case "2":
                        break;
                    case "1":
                    case "3":
                        startActivityForResult(new Intent(v.getContext(), ReviewProgressActivity.class), REQUEST_ID);
                        break;
                    case "4":
                        startActivityForResult(new Intent(v.getContext(), GoodsBossActivity.class), REQUEST_ID);
                        break;
                }
                break;
            //我发布的货源
            case R.id.tv_push_goods:
                startActivity(new Intent(v.getContext(), MineGoodsSourceActivity.class));
                break;
            //我发布的车源
            case R.id.tv_push_car:
                startActivity(new Intent(v.getContext(), MineCarSourceActivity.class));
                break;
            //我的抢单
            case R.id.tv_order:
                startActivity(new Intent(v.getContext(), MineRushActivity.class));
                break;
            //联系平台
            case R.id.tv_phones:
                phoneData._loadData();
                break;
            //使用指南
            case R.id.tv_use:
                startActivity(new Intent(v.getContext(), UseManagerActivity.class));
                break;
            //邀请好友
            case R.id.tv_friend:
                startActivity(new Intent(v.getContext(), InviteFriendActivity.class));
                break;
            case R.id.iv_message:
                startActivity(new Intent(v.getContext(), MessageActivity.class));
                break;
        }
    }

    public void showPhones(List<PhoneData> list) {
        if (_Lists.isEmpty(list)) {
            _showToast("暂无服务专员电话");
            return;
        }
        final BottomListDialog dialog = new BottomListDialog(getActivity());
        dialog.setData(list, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                _Intents.dialPhone(getActivity(), ((PhoneData) adapterView.getItemAtPosition(i)).mobile);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public void refreshInfo() {
        if (userData != null) {
            if (userData._hasCache()) {
                if (!"2".equals(String.valueOf(UserManager.getInstance().approve))) {
                    userData._loadData();
                }
            } else {
                userData._loadData();
            }
        }
    }
}
